<?php

class ClassBuilder{
	
	public $Logger;
	private $conn;
	public $DB_PLATFORM;
	public $SqlBuilder;
	public $DB_HOST;
	public $DB_USER;
	public $DB_PASS;
	
	
	public function createClassFromTable($tableName){
			try{
					switch($this->DB_PLATFORM){
					case DatabaseType::MySQL:
						$this->conn = mysql_connect($this->DB_HOST, $this->DB_USER, $this->DB_PASS) or die(mysql_error());
						mysql_select_db('information_schema', $this->conn) or die(mysql_error());
						//Selects Information from the information_schema 
						$this->createClassFolders();
						$this->SqlBuilder->Table('COLUMNS');
						$this->SqlBuilder->Where('TABLE_NAME','=',$tableName);
						$results = mysql_query($this->SqlBuilder->SELECTStatement());
						$Columns = array();
						while ($row = mysql_fetch_assoc($results)) {
							if ($row['COLUMN_KEY'] =="PRI")
							{
								$prim = $row['COLUMN_NAME'];
							}
							array_push($Columns,$row['COLUMN_NAME']);
						}
						$this->createRLFile($tableName, $Columns,$prim);
					break;
					default:
						throw new Exception('The Function createClassFromTable only works on MySql database');
					break;	
					}
			}catch (Exception $e){
				App::$Logger->Log(LogType::Error, $e->getMessage());	
			}
		}
		
		private function createClassFolders(){
				if(is_Dir('Classes') != true){
					mkdir('Classes');	
				}
		}
		
		private function createRLFile($tableName, $Columns, $prim){
			$classFile = fopen('Classes/'.$this->cleanNames($tableName).".php",'w');
			fwrite($classFile,"<?php\r\n");
			fwrite($classFile,"\r\n");
			fwrite($classFile,"function __autoload(\$class_name) {\r\n");
			fwrite($classFile,"\tinclude '../'.\$class_name . '.php';\r\n");
			fwrite($classFile,"}\r\n");
			fwrite($classFile,"\r\n");
			fwrite($classFile,"class ".$this->cleanNames($tableName)." {\r\n");
			
			fwrite($classFile,"\t//  Region:  DB / DB Fields\r\n");
			fwrite($classFile,"\tconst __TBL = ".$tableName.";\r\n");
			foreach($Columns as &$value){
				fwrite($classFile,"\tconst __FLD_".strtoupper($value)." = \"".$value."\";\r\n");	
			}
			fwrite($classFile,"\tprivate \$conn;\r\n");
			fwrite($classFile,"\t//  Region:  DB / DB Fields End\r\n");
			fwrite($classFile,"\r\n");
			fwrite($classFile,"\t//  Region:  Properties\r\n");
			foreach ($Columns as &$value) {
    			fwrite($classFile, "\tpublic $".$this->cleanNames($value).";\r\n");
			}
			fwrite($classFile,"\tpublic \$isLoaded;\r\n");
			fwrite($classFile,"\r\n");
			//Construct Class
			fwrite($classFile,"\tfunction __construct() {\r\n");
				fwrite($classFile,"\t\t\$this->conn =  App::getDB();\r\n");
			fwrite($classFile,"\t}\r\n");
			fwrite($classFile,"\r\n");
			
			//Populate Function
			fwrite($classFile,"// Region: Standard Functions\r\n");
			fwrite($classFile,"\tpublic function populate(\$PrimKeyValue){\r\n");
			
			fwrite($classFile,"\t\tApp::\$DAL->SqlBuilder->Table(self::__TBL);\r\n");
			foreach ($Columns as &$value) {
				if ($value == $prim){
					fwrite($classFile,"\t\tApp::\$DAL->SqlBuilder->Where(self::__FLD_".strtoupper($value).",\"=\",\$prinKeyValue);\r\n");
				}
			}
			fwrite($classFile,"\t\t\$row = mysql_fetch_array(App::\$DAL->SqlBuilder->SELECTStatement());\r\n");
			foreach ($Columns as &$value) {
				fwrite($classFile,"\t\t\$this->".$this->cleanNames($value)." = \$row[self::__FLD_".strtoupper($value)."];\r\n");
			}
			fwrite($classFile,"\t\t\$this->isLoaded = true;\r\n");
			fwrite($classFile,"\t}\r\n");
			
			//Save function
			fwrite($classFile,"\r\n");
			fwrite($classFile,"\tpublic function save() {\r\n");
			fwrite($classFile,"\t\tApp::\$DAL->SqlBuilder->Table(self::__TBL);\r\n");
			foreach ($Columns as &$value) {
			fwrite($classFile,"\t\tApp::\$DAL->SqlBuilder->Field(self::__FLD_".strtoupper($value).",\$this->".$this->cleanNames($value).");\r\n");
			}
			fwrite($classFile,"\t\tif (\$this->isLoaded === true) {\r\n");
				//Add Where Statment for Update
				foreach ($Columns as &$value) {
					if ($value == $prim){
					fwrite($classFile,"\t\t\tApp::\$DAL->SqlBuilder->Where(self::__FLD_".strtoupper($value).",\"=\",\$this->".$this->cleanNames($value).");\r\n");
					}
				}
				//
				fwrite($classFile,"\t\t\t\$SQL = App::\$DAL->SqlBuilder->UPDATEStatement();\r\n");
			fwrite($classFile,"\t\t} else {\r\n");
				fwrite($classFile,"\t\t\t\$SQL = App::\$DAL->SqlBuilder->INSERTStatement();\r\n");
			fwrite($classFile,"\t\t}\r\n");
		fwrite($classFile,"\t\tApp::\$DAL->Execute(\$SQL);\r\n");
	fwrite($classFile,"\t}\r\n");
			
			//Close Class
			fwrite($classFile,"}\r\n");
			fwrite($classFile,"?>");
			return "Class Created";
		}
		
		
		private function cleanNames($name){
			$temp = str_replace("_", " ",$name);
			$temp = ucwords(strtolower($temp));
			$temp = str_replace(" ","",$temp);
			return $temp;
		}
}
?>